<html lang="en">
<head>
    <title>气清商城</title>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="./images/favicon.ico"/>
    <!--    <link rel="shortcut icon" type="image/x-icon" href="./images/favicon.png"/>-->
    <!--    <link rel="shortcut icon" href="./images/favicon.ico"/>-->
    <!--    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,300i,400,400i,500,500i,600,600i,700,700i&display=swap"-->
    <!--          rel="stylesheet">-->
    <!--    <link href="https://fonts.googleapis.com/css?family=Lato:300,300i,400,400i,700,700i&display=swap" rel="stylesheet">-->
    <link rel="stylesheet" href="./css/bootstrap.min.css">
    <link rel="stylesheet" href="./css/font-awesome.min.css">
    <link rel="stylesheet" href="./css/owl.carousel.min.css">
    <link rel="stylesheet" href="./css/animate.min.css">
    <link rel="stylesheet" href="./css/jquery-ui.css">
    <link rel="stylesheet" href="./css/slick.css">
    <link rel="stylesheet" href="./css/chosen.min.css">
    <link rel="stylesheet" href="./css/pe-icon-7-stroke.css">
    <link rel="stylesheet" href="./css/magnific-popup.min.css">
    <link rel="stylesheet" href="./css/lightbox.min.css">
    <link rel="stylesheet" href="./js/fancybox/source/jquery.fancybox.css">
    <link rel="stylesheet" href="./css/jquery.scrollbar.min.css">
    <link rel="stylesheet" href="./css/mobile-menu.css">
    <link rel="stylesheet" href="./fonts/flaticon/flaticon.css">
    <link rel="stylesheet" href="./css/style.css">
    <script src="./js/vue.min.js"></script>
    <style> [v-cloak] {
        display: none;
    } </style>
</head>
<body class="inblog-page">
<header class="header style7">
    <div class="top-bar">
        <div class="container">
            <div class="top-bar-left">
                <div class="header-message">
                    欢迎来到气清!
                </div>
            </div>
            <div class="top-bar-right">
                <div id="loginbar">
                    <ul class="header-user-links">
                        <li>
                            <a href="http://www.qk.com/login">请登录</a>
                        </li>
                    </ul>
                    <ul class="header-user-links">
                        <li>
                            <a href="http://www.qk.com/regist">注册</a>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </div>
    <div class="header-nav-container">
        <div class="container">
            <div class="header-nav-wapper main-menu-wapper">
                <div class="header-nav">
                    <div class="container-wapper">
                        <ul class="teamo-clone-mobile-menu teamo-nav main-menu " id="menu-main-menu">
                            <li class="menu-item  menu-item-has-children">
                                <a href="http://www.qk.com/allproducts" class="teamo-menu-item-title"
                                   title="Home">全部商品</a>
                            </li>
                            <li class="menu-item menu-item-has-children">
                                <a href="http://www.qk.com/shoppingcart" class="teamo-menu-item-title"
                                   title="Shop">购物车</a>
                            </li>
                            <li class="menu-item  menu-item-has-children item-megamenu">
                                <a href="http://www.qk.com/order" class="teamo-menu-item-title" title="Pages">订单</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="main-header">
                <div class="row">
                    <div class="col-lg-3 col-sm-4 col-md-3 col-xs-7 col-ts-12 header-element">
                        <div class="logo">
                            <a href="http://www.qk.com/index">
                                <img src="./images/logo.png" alt="img">
                            </a>
                        </div>
                    </div>
                    <div class="col-lg-7 col-sm-8 col-md-6 col-xs-5 col-ts-12">
                        <div class="block-search-block">
                            <form class="form-search form-search-width-category">
                                <div class="form-content">
                                    <div class="inner">
                                        <input type="text" class="input" id="productName" value="" placeholder="输入商品名称">
                                    </div>
                                    <button class="btn-search" type="button" id="searchProduct">
                                        <span class="icon-search"></span>
                                    </button>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</header>

<div class="site-content">
    <main class="site-main  main-container no-sidebar">
        <div class="container">
            <div class="breadcrumb-trail breadcrumbs">
                <ul class="trail-items breadcrumb">
                    <li class="trail-item trail-begin">
                        <a href="">
								<span>
									主页
								</span>
                        </a>
                    </li>
                    <li class="trail-item trail-end active">
							<span>
								我的个人中心
							</span>
                    </li>
                </ul>
            </div>
            <div class="row">
                <div class="main-content-cart main-content col-sm-12">
                    <div class="customer_login">
                        <div class="row">
                            <div class="col-lg-6 col-md-6 col-sm-12">
                                <div class="login-item">
                                    <h5 class="title-login">个人中心</h5>
                                    <form class="register" id="personal" v-if="user">
                                        <p class="form-row form-row-wide">
                                            <label class="text">邮箱</label>
                                            <input id="email" title="email" type="email" v-model="user.email"
                                                   class="input-text">
                                        </p>
                                        <p class="form-row form-row-wide">
                                            <label class="text">手机号码</label>
                                            <input id="phone" title="phone" type="phone" v-model="user.phone"
                                                   class="input-text">
                                        </p>
                                        <p class="form-row form-row-wide">
                                            <label class="text">用户名</label>
                                            <input id="name" title="name" type="text" v-model="user.username"
                                                   class="input-text" disabled=“disabled”>
                                        </p>
                                        <p class="form-row form-row-wide">
                                            <label class="text">密码</label>
                                            <input id="password" title="pass" type="password" v-model="user.password"
                                                   class="input-text">
                                        </p>
                                        <p class="form-row form-row-wide">
                                            <label class="text">确认密码</label>
                                            <input id="pass2" title="passagain" type="password" class="input-text">
                                        </p>
                                    </form>
                                    <div class="input-group-btn">
                                        <button onclick="checkForm()" class="button-submit">提交修改</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>
</div>


<script src="./js/jquery-1.12.4.min.js"></script>
<script src="./js/jquery.plugin-countdown.min.js"></script>
<script src="./js/jquery-countdown.min.js"></script>
<script src="./js/bootstrap.min.js"></script>
<script src="./js/owl.carousel.min.js"></script>
<script src="./js/magnific-popup.min.js"></script>
<script src="./js/isotope.min.js"></script>
<script src="./js/jquery.scrollbar.min.js"></script>
<script src="./js/jquery-ui.min.js"></script>
<script src="./js/mobile-menu.js"></script>
<script src="./js/chosen.min.js"></script>
<script src="./js/slick.js"></script>
<script src="./js/jquery.elevateZoom.min.js"></script>
<script src="./js/jquery.actual.min.js"></script>
<script src="./js/fancybox/source/jquery.fancybox.js"></script>
<script src="./js/lightbox.min.js"></script>
<script src="./js/owl.thumbs.min.js"></script>
<script src="./js/jquery.scrollbar.min.js"></script>
<!--<script src='https://ditu.google.cn/maps/api/js?key=AIzaSyC3nDHy1dARR-Pa_2jjPCjvsOR4bcILYsM'></script>-->
<script src="./js/frontend-plugin.js"></script>
<script src="./js/jquery.cookie.js"></script>
<script type="text/javascript">
    var TT = QK = {
        checkLogin: function () {
            var _ticket = $.cookie("QK_TICKET");

            if (!_ticket) {
                return;
            }
            //当dataType类型为jsonp时，jQuery就会自动在请求链接上增加一个callback的参数
            $.ajax({
                url: "http://sso.qk.com/user/query/" + _ticket,
                dataType: "jsonp",
                type: "GET",
                success: function (data) {
                    if (data.status == 200) {
                        //要求获取的是user的JSON串
                        var _data = JSON.parse(data.data);//将JSON串转化为对象
                        var html = "<div class='top-bar-right'><div id='loginbar'><ul class='header-user-links'><li><a href=\"/user/logout\" class=\"link-logout\"> " + _data.username + ",[退出]</a></li></ul><ul class=\"header-user-links\"> <li><a href=\"http://www.qk.com/personal\" class=\"link-setting\">个人中心</a></li></ul> </div> </div> ";
                        $("#loginbar").html(html);
                    }
                }
            });
        }
    }
    // <a href="http://www.jt.com/user/logout" class="link-logout">[退出]</a> <a>个人中心</a>


    $(function () {
        // 查看是否已经登录，如果已经登录查询登录信息
        TT.checkLogin();
    });

    $('#searchProduct').click(function () {
        title = $('#productName').val();
        console.log(title);
        window.location.href = "http://www.qk.com/allproducts?" + title;
    })

    function logout() {
        $.ajax({
            async: false,
            url: '/personal/logout',
            dataType: 'json',
            success: function (result) {
               if (result.status ==200){
                   location.href = 'login';
               }
            }
        })
    }


    var vm = new Vue({
        el: '#personal',
        data: {
            user: null,
        },
    });


    init();

    //渲染个人数据
    function init() {
        $.ajax({
            async: false,
            url: '/personal/show',
            dataType: 'json',
            success: function (result) {
                resData = result.data;
                vm.user = resData;
            }

        })
    }

    function updatePersonal() {
        // 对数据进行验证
        var email = $('#email').val();
        var phone = $('#phone').val();
        var name = $('#name').val();
        var password = $('#pass2').val();


        var user = {
            'email': email,
            'phone': phone,
            'username': name,
            'password': password,
        }

        $.ajax({
            async: false,
            url: '/personal/update',
            dataType: "json",
            type: "post",
            data: user,
            success: function (result) {
                console.log(result)
                if (result.status == 200) {
                    window.alert("修改成功！")
                    logout();
                } else {
                    window.alert("修改失败！")
                }
            }
        })
    }


    function checkForm() {
        var email = $('#email').val();
        var phone = $('#phone').val();
        var password = $('#password').val();
        var pass2 = $('#pass2').val();
        var user = {
            'email': email,
            'phone': phone,
            'password': pass2
        };
        if (checkEmail(email) && checkPhone(phone) && checkPass(pass2)) {

            return updatePersonal();

        } else {
            return false;
        }


    }

    //对两个密码进行一样的校验
    function checkPass(pass2) {
        if (pass2 == null || pass2 == "") {
            return true
        } else if (pass2 != 0 && pass2.length < 6) {
            alert("密码长度大于6!");
            $('#pass2').focus();
            return false;
        } else {
            return true
        }
    }

    //对phone进行check
    function checkPhone(phone) {
        if (phone == null || phone == "") {
            alert("手机号码不能为空!");
            $('#phone').focus();
            return false;
        } else if (!isPhone(phone)) {
            alert("您输入的手机号码有误,请重新核对后再输入!");
            $('#phone').focus();
            return false;
        } else {
            return true;
        }
    }

    //判断phone的正则表达式
    function isPhone(str) {
        var reg = /^1[0-9]{10}$/;
        return reg.test(str);
    }


    //对email进行check
    function checkEmail(email) {
        if (email == null || email == "") {
            alert("邮箱不能为空!");
            $('#email').focus();
            return false;
        } else if (!isEmail(email)) {
            alert("您输入的邮箱有误,请重新核对后再输入!");
            $('#email').focus();
            return false;
        } else {
            return true;
        }

    }

    //判断email的正则表达式
    function isEmail(email) {
        var reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
        return reg.test(email);
    }

</script>
</body>
</html>